/*
 * Copyright (c) 2024 Black Sesame Technologies
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <string.h>
#include "dal/allocator.h"
#include "dal/utils.h"

void* dal_allocate(size_t size)
{
    dal_utils_allocator_t allocator = dal_utils_get_allocator();
    void* ptr = allocator.allocate(size);
    if (ptr) {
        memset(ptr, 0, size);
    }
    return ptr;
}

void dal_free(void* ptr)
{
    dal_utils_allocator_t allocator = dal_utils_get_allocator();
    allocator.deallocate(ptr);
}

#define TEMPLATE_ALLOC_AND_FREE(Type)                                                              \
    dal_##Type##_t* dal_##Type##_allocate()                                                        \
    {                                                                                              \
        dal_utils_allocator_t allocator = dal_utils_get_allocator();                               \
        return (dal_##Type##_t*)dal_allocate(sizeof(dal_##Type##_t));                              \
    }                                                                                              \
                                                                                                   \
    void dal_##Type##_free(dal_##Type##_t* ptr)                                                    \
    {                                                                                              \
        dal_free(ptr);                                                                             \
    }

TEMPLATE_ALLOC_AND_FREE(domain_participant)
TEMPLATE_ALLOC_AND_FREE(topic)
TEMPLATE_ALLOC_AND_FREE(publisher)
TEMPLATE_ALLOC_AND_FREE(subscriber)
TEMPLATE_ALLOC_AND_FREE(datawriter)
TEMPLATE_ALLOC_AND_FREE(datareader)
TEMPLATE_ALLOC_AND_FREE(service)
TEMPLATE_ALLOC_AND_FREE(client)
TEMPLATE_ALLOC_AND_FREE(domain_participant_listener)
TEMPLATE_ALLOC_AND_FREE(topic_listener)
TEMPLATE_ALLOC_AND_FREE(publisher_listener)
TEMPLATE_ALLOC_AND_FREE(subscriber_listener)
TEMPLATE_ALLOC_AND_FREE(datawriter_listener)
TEMPLATE_ALLOC_AND_FREE(datareader_listener)
TEMPLATE_ALLOC_AND_FREE(domain_participant_qos)
TEMPLATE_ALLOC_AND_FREE(topic_qos)
TEMPLATE_ALLOC_AND_FREE(publisher_qos)
TEMPLATE_ALLOC_AND_FREE(subscriber_qos)
TEMPLATE_ALLOC_AND_FREE(datawriter_qos)
TEMPLATE_ALLOC_AND_FREE(datareader_qos)
TEMPLATE_ALLOC_AND_FREE(type_support)
